#!/bin/bash
if [ ! -d "entries" ]; then
        echo "You haven't initialized nci system yet... Please use \"./init\" before editing entries."
        exit 1
fi
if [ ! -f "user.conf" ]; then
        echo "\"user.conf\" file doesn't exist, please run \"./init\" (your existing files won't be affected)"
        exit 1
fi

#check if no id entered.
if [ -z "$1" ]; then
	echo "Error: Please type the ID of the entry you want to edit (Usage: \"./mod <entry_id>\")."
	exit 1
fi

editor=""
read -r editor < "user.conf"
#check if entry is not deleted, if exists, open the editor
if [ -f "entries/entry_$1" ]; then
	contents=$(cat "entries/entry_$1")
	if [ "$contents" = "[DELETED]" ]; then
		echo "Error: That entry is deleted."
		exit 1
	else
		#change this for your favorite text editor if u want
		eval "$editor entries/entry_$1"
		echo "Remember to run \"./refresh\" to save changes."
	fi
else
	echo "Error: That entry doesn't exist."
fi
